using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class SayHelloPage : BasePage, FMsgChange
	{
		private const string TAG = "LbsSayHelloPage";
		private const int PER_LOAD_NUM = 10;
		private EventWatcher mWatcherHeadImgAdd;
		private ObservableCollection<ChatMsg> curSayHelloDataList;
		private ApplicationBarIconButton clearSayHelloItem;
		private ApplicationBar mBar;
		internal Grid LayoutRoot;
		internal Grid listSayHelloView;
		internal ListBox sayHelloListBox;
		internal TextBlock txtSayHelloListEmpty;
		private bool _contentLoaded;
		public SayHelloPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Page_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Page_Unloaded));
			base.set_ApplicationBar(this.GetApplicationBar());
		}
		private void Page_Loaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("LbsSayHelloPage", "Page_Loaded");
			SayHelloListItemCtrl.ShowInViewItemsCachedImg();
			this.initEventWatcher();
		}
		private void Page_Unloaded(object sender, RoutedEventArgs e)
		{
			Log.smethod_5("LbsSayHelloPage", "Page_Unloaded");
			this.unInitEventWatcher();
		}
		private void initEventWatcher()
		{
			if (this.mWatcherHeadImgAdd == null)
			{
				this.mWatcherHeadImgAdd = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerHeadImgAdd));
			}
			EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
		}
		private void unInitEventWatcher()
		{
			if (this.mWatcherHeadImgAdd != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.mWatcherHeadImgAdd);
				this.mWatcherHeadImgAdd = null;
			}
		}
		private void onEventHandlerHeadImgAdd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<string> listObject = evtArgs.getListObject<string>();
			if (listObject == null)
			{
				return;
			}
			List<string> list = new List<string>();
			using (List<string>.Enumerator enumerator = listObject.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current != null && current.GetType() == typeof(string))
					{
						list.Add(current);
					}
				}
			}
			SayHelloListItemCtrl.RefreshHeadImgInView(list);
			this.sayHelloListBox.UpdateLayout();
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("LbsSayHelloPage", "OnPageIn");
			this.initSayHelloMsg();
			FMsgMgr.addObserver(this);
			FMsgMgr.setNewMsgNum(FMsgType.const_0, 0);
			this.showEmptyViewIfNeed();
		}
		protected override void OnPageOut()
		{
			Log.smethod_5("LbsSayHelloPage", "OnPageOut");
			FMsgMgr.removeObserver(this);
			this.clearSayHelloData();
		}
		public void onFMsgNewNumChange(FMsgType type)
		{
		}
		public void onFMsgDel(ChatMsg chatMsg_0, FMsgType type)
		{
		}
		public void onFMsgAdd(ChatMsg chatMsg_0, FMsgType type)
		{
			if (type == FMsgType.const_0 && chatMsg_0 != null)
			{
				base.get_Dispatcher().BeginInvoke(delegate
				{
					if (this.curSayHelloDataList == null)
					{
						this.curSayHelloDataList = new ObservableCollection<ChatMsg>();
					}
					this.curSayHelloDataList.Insert(0, chatMsg_0);
					this.sayHelloListBox.set_ItemsSource(this.curSayHelloDataList);
					this.showEmptyViewIfNeed();
					FMsgMgr.setNewMsgNum(FMsgType.const_0, 0);
				});
				return;
			}
		}
		private void clearSayHelloData()
		{
			if (this.curSayHelloDataList != null)
			{
				this.curSayHelloDataList.Clear();
			}
		}
		private bool hasMoreSayHelloMsg()
		{
			int msgNum = FMsgMgr.getMsgNum(FMsgType.const_0);
			int num = (this.curSayHelloDataList != null) ? this.curSayHelloDataList.get_Count() : 0;
			return msgNum > num;
		}
		private void showEmptyViewIfNeed()
		{
			if (this.SayHelloDataIsEmpty())
			{
				this.sayHelloListBox.set_Visibility(1);
				this.txtSayHelloListEmpty.set_Visibility(0);
				this.clearSayHelloItem.set_IsEnabled(false);
				return;
			}
			this.sayHelloListBox.set_Visibility(0);
			this.txtSayHelloListEmpty.set_Visibility(1);
			this.clearSayHelloItem.set_IsEnabled(true);
		}
		private bool SayHelloDataIsEmpty()
		{
			return this.curSayHelloDataList == null || this.curSayHelloDataList.get_Count() == 0;
		}
		private void addDisplayMoreIfneed()
		{
			if (this.curSayHelloDataList != null && this.curSayHelloDataList.get_Count() > 0 && this.hasMoreSayHelloMsg())
			{
				this.curSayHelloDataList.Add(SayHelloListItemCtrl.displayMoreMsg);
			}
		}
		private void initSayHelloMsg()
		{
			if (this.curSayHelloDataList != null && this.curSayHelloDataList.get_Count() != 0)
			{
				this.curSayHelloDataList.Clear();
			}
			int newMsgNum = FMsgMgr.getNewMsgNum(FMsgType.const_0);
			if (newMsgNum > 0)
			{
				List<ChatMsg> msgList = FMsgMgr.getMsgList(FMsgType.const_0, 0, newMsgNum, false);
				if (msgList != null && msgList.get_Count() > 0)
				{
					this.curSayHelloDataList = new ObservableCollection<ChatMsg>(msgList);
					this.addDisplayMoreIfneed();
				}
				else
				{
					FMsgMgr.setNewMsgNum(FMsgType.const_0, 0);
				}
			}
			else
			{
				int msgNum = FMsgMgr.getMsgNum(FMsgType.const_0);
				if (msgNum > 0)
				{
					List<ChatMsg> msgList2 = FMsgMgr.getMsgList(FMsgType.const_0, 0, msgNum, false);
					if (msgList2 != null && msgList2.get_Count() > 0)
					{
						this.curSayHelloDataList = new ObservableCollection<ChatMsg>(msgList2);
					}
					else
					{
						FMsgMgr.setNewMsgNum(FMsgType.const_0, 0);
					}
				}
			}
			this.sayHelloListBox.set_ItemsSource(this.curSayHelloDataList);
		}
		private List<ChatMsg> getLeftSayHelloMsg(int loadLimit)
		{
			int num = 0;
			if (this.curSayHelloDataList != null)
			{
				num = this.curSayHelloDataList.get_Count();
			}
			int msgNum = FMsgMgr.getMsgNum(FMsgType.const_0);
			int num2 = msgNum - num;
			List<ChatMsg> result = null;
			int num3 = (num2 > loadLimit) ? loadLimit : num2;
			if (num3 > 0)
			{
				result = FMsgMgr.getMsgList(FMsgType.const_0, num, num3, false);
			}
			return result;
		}
		private void SayHelloList_ItemClick(object sender, object object_0)
		{
			if (object_0 == null)
			{
				return;
			}
			ChatMsg chatMsg = object_0 as ChatMsg;
			SayHelloListItemCtrl sayHelloListItemCtrl = sender as SayHelloListItemCtrl;
			if (chatMsg != SayHelloListItemCtrl.displayMoreMsg)
			{
				FMessageValidInfo fMsg = sayHelloListItemCtrl.fMsg;
				if (fMsg == null)
				{
					return;
				}
				MainPage mainPageInstance = MainPage.mainPageInstance;
				if (mainPageInstance == null)
				{
					return;
				}
				if (fMsg != null)
				{
					FMessageValidInfo fMessageValidInfo = fMsg;
					if (fMessageValidInfo == null)
					{
						return;
					}
					ContactDetailViewArgs conDetailArgFromFMessageValidInfo = ContactDetailViewForAll.getConDetailArgFromFMessageValidInfo(fMessageValidInfo);
					mainPageInstance.GoToPage("/Source/UI/Contact/ContactDetailViewForAll.xaml", new object[]
					{
						conDetailArgFromFMessageValidInfo
					}, false);
					return;
				}
			}
			else
			{
				if (sayHelloListItemCtrl != null && sayHelloListItemCtrl.progressBar.get_Visibility() == 1)
				{
					sayHelloListItemCtrl.LayoutShowMore.set_Background(null);
					sayHelloListItemCtrl.txtShowMore.set_Visibility(1);
					sayHelloListItemCtrl.progressBar.set_Visibility(0);
					sayHelloListItemCtrl.progressBar.IsIndeterminate = true;
					base.get_Dispatcher().BeginInvoke(delegate
					{
						this.curSayHelloDataList.RemoveAt(this.curSayHelloDataList.get_Count() - 1);
						List<ChatMsg> leftSayHelloMsg = this.getLeftSayHelloMsg(10);
						using (List<ChatMsg>.Enumerator enumerator = leftSayHelloMsg.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								ChatMsg current = enumerator.get_Current();
								this.curSayHelloDataList.Add(current);
							}
						}
						this.addDisplayMoreIfneed();
					});
				}
			}
		}
		public IApplicationBar GetApplicationBar()
		{
			this.mBar = new ApplicationBar();
			this.mBar.set_IsVisible(true);
			base.setAppBarStyle(this.mBar);
			this.mBar.set_Mode(0);
			this.clearSayHelloItem = new ApplicationBarIconButton(new Uri("/Resource/Img/Plugin/Shake/icon_clearData.png", 2));
			this.clearSayHelloItem.set_Text(strings.Shake_Clear_List);
			this.clearSayHelloItem.add_Click(new EventHandler(this.clearSayHelloData_Click));
			this.mBar.get_Buttons().Add(this.clearSayHelloItem);
			return this.mBar;
		}
		private void clearSayHelloData_Click(object sender, EventArgs e)
		{
			MMMessageBox mMMessageBox = MMMessageBox.Show(null, strings.Shake_List_Clear_Tip, true, strings.Shake_LBS_ClearDataButtonTitle, strings.COM_Cancel);
			mMMessageBox.set_FontSize(22.0);
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.ClearSayHelloDataHandler);
		}
		public void ClearSayHelloDataHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				this.showProgressCover(strings.Shake_List_Clearing);
				base.get_Dispatcher().BeginInvoke(delegate
				{
					FMsgMgr.CleanByType(FMsgType.const_0);
					this.clearSayHelloData();
					this.txtSayHelloListEmpty.set_Visibility(0);
					this.sayHelloListBox.set_Visibility(1);
					this.clearSayHelloItem.set_IsEnabled(false);
					this.hideProgressCover();
					MessageBoxManager.ShowToast(strings.Shake_Clear_List_OK, 2500, null, false);
				});
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, (SolidColorBrush)Application.get_Current().get_Resources().get_Item("HalfOpacityBrush1"), false, false, null);
			this.mBar.set_IsVisible(false);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
			this.mBar.set_IsVisible(true);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Lbs/UI/SayHelloPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.listSayHelloView = (Grid)base.FindName("listSayHelloView");
			this.sayHelloListBox = (ListBox)base.FindName("sayHelloListBox");
			this.txtSayHelloListEmpty = (TextBlock)base.FindName("txtSayHelloListEmpty");
		}
	}
}
